﻿/****************************** Module Header ******************************\
 * Module Name:  SoPager.cs
 * Module Version:  1.0.0 (2012-9-19)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the FormPagger.
 * # 1.0.1 (2013-1-25) 新增右键菜单执行功能
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Windows.Forms;
using UCL;

namespace UCL.Forms.Components
{
    /// <summary>
    /// 获取数据源的委托方法
    /// </summary>
    /// <param name="recordcount"></param>
    /// <param name="pagecount"></param>
    /// <param name="sql"></param>
    /// <param name="pageid"></param>
    /// <param name="pagesize"></param>
    /// <returns></returns>
    public delegate object GetDataSource(out int recordcount, out int pagecount, string sql, int pageid, int pagesize);

    /// <summary>
    /// WinForm通用分页控件
    /// </summary>
    public partial class SoPager : UserControl, IPager
    {
        /// <summary>
        /// 外部分页处理方法(解决WCF资源占用问题)
        /// <para>参数1是pageid,参数2是pagesize</para>
        /// <para>如：</para>
        /// <para>formPagger1.PagingHandler = (int pageid, int pagesize) =&gt;</para>
        /// <para>{</para>
        /// <para>   int recordcount, pagecount;</para>
        /// <para>   dgvList.DataSource = SRC.Current.Call_GetCallRecordPagingList(out recordcount, out pagecount, "", pageid, pagesize);</para>
        /// <para>   SRC.Close();</para>
        /// <para>   formPagger1.Config(recordcount, pagecount, pageid, pagesize);</para>
        /// <para>};</para>
        /// <para>formPagger1.PagingHandler(1, 20);</para>
        /// <para>或者</para>
        /// <para>formPagger1.PagingHandler = (int pageid, int pagesize) =&gt;</para>
        /// <para>{</para>
        /// <para>    int recordcount = 0, pagecount = 0;</para>
        /// <para>    object[] args = new object[] { ConfigData.ConnectionName, "from tGeneralData where ExTags='ORDER_ICCID' and State=1", "CreatedTime desc", "Name, Value, CreatedTime", pageid, pagesize, recordcount, pagecount };</para>
        /// <para>    DataTable dt = WSDLHelper.InvokeMethod&lt;DataTable&gt;("ExecutePagingDataTable", args);</para>
        /// <para>    if (dt == null)</para>
        /// <para>    {</para>
        /// <para>        WSDLHelper.ShowMessage("Load_Data");</para>
        /// <para>    }</para>
        /// <para>    c = (int)args[5];</para>
        /// <para>    d = (int)args[6];</para>
        /// <para>    dgvList.DataSource = dt;</para>
        /// <para>    formPagger1.Config(recordcount, pagecount, pageid, pagesize);</para>
        /// <para>};</para>
        /// <para>formPagger1.PagingHandler(1, 25);</para>
        /// </summary>
        public Action<int, int> PagingHandler { get; set; }
        public SoPager()
        {
            InitializeComponent();

            // 文本控件调整尺寸的时候自动调整大小
            lblResizeConfig();
            // 分页跳转
            lnkClickConfig();
            // 通过文本框更改分页参数
            tbKeyPressConfig();
            // 上下文右键菜单点击事件配置
            cmiClickConfig();
        }

        // 文本控件调整尺寸的时候自动调整大小 
        private void lblResizeConfig()
        {
            lblPageCount.Resize += (object sender, EventArgs e) => { ResizeControlWidth(); };
            lblPageCount.Resize += (object sender, EventArgs e) => { ResizeControlWidth(); };
            lnkFirstPage.Resize += (object sender, EventArgs e) => { ResizeControlWidth(); };
            lnkPrevPage.Resize += (object sender, EventArgs e) => { ResizeControlWidth(); };
            lnkNextPage.Resize += (object sender, EventArgs e) => { ResizeControlWidth(); };
            lnkLastPage.Resize += (object sender, EventArgs e) => { ResizeControlWidth(); };
        }

        // 分页跳转
        private void lnkClickConfig()
        {
            // 跳转到第一页
            lnkFirstPage.LinkClicked += (object sender, LinkLabelLinkClickedEventArgs e) => { this.Paging(this.FirstPage, this.PageSize); };
            // 跳转到上一页
            lnkPrevPage.LinkClicked += (object sender, LinkLabelLinkClickedEventArgs e) => { this.Paging(this.PrevPage, this.PageSize); };
            // 跳转到下一页
            lnkNextPage.LinkClicked += (object sender, LinkLabelLinkClickedEventArgs e) => { this.Paging(this.NextPage, this.PageSize); };
            // 跳转到最末页
            lnkLastPage.LinkClicked += (object sender, LinkLabelLinkClickedEventArgs e) => { this.Paging(this.LastPage, this.PageSize); };
        }

        // 通过文本框更改分页参数
        private void tbKeyPressConfig()
        {
            // 更改分页尺寸
            tbPageSize.KeyPress += (object sender, KeyPressEventArgs e) =>
            {
                if (e.KeyChar == 13)
                {
                    int pagesize = tbPageSize.Text.ToInt();
                    if (pagesize <= 0) return;
                    this.Paging(this.FirstPage, pagesize);
                }
            };
            // 更改当前页码
            tbPageIndex.KeyPress += (object sender, KeyPressEventArgs e) =>
            {
                if (e.KeyChar == 13)
                {
                    int pageindex = tbPageIndex.Text.ToInt();
                    if (pageindex <= 0) return;
                    this.Paging(pageindex, this.PageSize);
                }
            };
        }

        // 上下文右键菜单点击事件配置
        private void cmiClickConfig()
        {
            // 执行菜单点击事件
            cmi执行ToolStripMenuItem.Click += (object sender, EventArgs e) =>
            {
                int pageindex = tbPageIndex.Text.ToInt();
                if (pageindex <= 0) return;
                this.Paging(pageindex, this.PageSize);
            };
        }

        int width;
        /// <summary>
        /// 重新设置控件尺寸
        /// </summary>
        /// <remarks>2014-2-17 by sutroon</remarks>
        private void ResizeControlWidth()
        {
            // 共0条记录，每页[]条,当前第[]0/0页 第一页，上一页，下一页，最末页
            width = 0;
            if (lbl共.Visible) width += lbl共.Width; // 共
            if (lblRecordCount.Visible) width += lblRecordCount.Width; // 0
            if (lbl条记录.Visible) width += lbl条记录.Width; // 条记录,
            if (lbl每页.Visible) width += lbl每页.Width; // 每页
            if (tbPageSize.Visible) width += tbPageSize.Width; // []
            if (lbl条.Visible) width += lbl条.Width; // 条
            if (lbl当前第.Visible) width += lbl当前第.Width; // 当前第
            if (tbPageIndex.Visible) width += tbPageIndex.Width; // []
            if (lblPageIndex.Visible) width += lblPageIndex.Width; // 0
            if (lbl页数分隔符.Visible) width += lbl页数分隔符.Width; // /
            if (lblPageCount.Visible) width += lblPageCount.Width; // 0
            if (lbl页.Visible) width += lbl页.Width; // 页
            if (lnkFirstPage.Visible) width += lnkFirstPage.Width;
            if (lnkPrevPage.Visible) width += lnkPrevPage.Width;
            if (lnkNextPage.Visible) width += lnkNextPage.Width;
            if (lnkLastPage.Visible) width += lnkLastPage.Width;
            this.Width = width;
        }

        #region IPager的实现

        /// <summary>
        /// 第一页页码
        /// </summary>
        public int FirstPage { get { return 1; } }

        /// <summary>
        /// 上一页页码
        /// </summary>
        public int PrevPage
        {
            get
            {
                return (this.PageIndex > 1) ? this.PageIndex - 1 : this.PageIndex;
            }
        }

        /// <summary>
        /// 下一页页码
        /// </summary>
        public int NextPage
        {
            get
            {
                return (this.PageIndex < this.PageCount) ? this.PageIndex + 1 : this.PageIndex;
            }
        }

        /// <summary>
        /// 最末页页码
        /// </summary>
        public int LastPage { get { return this.PageCount; } }

        /// <summary>
        /// 当前页页码
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 当前页列表中游标位置
        /// </summary>
        public int ListIndex { get; private set; }

        /// <summary>
        /// 记录集中游标位置
        /// </summary>
        public int RecordIndex { get; private set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; private set; }

        /// <summary>
        /// 分页尺寸
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount { get; private set; }

        /// <summary>
        /// 当前页记录数
        /// </summary>
        public int ListCount { get; private set; }


        public bool Next()
        {
            throw new NotImplementedException();
        }
        #endregion

        // 获取数据源的方法
        private GetDataSource fnc;

        /// <summary>
        /// 分页条所绑定的DataGridView对象
        /// </summary>
        private DataGridView dataGridView;

        // 当前执行的SQL语句
        private string sql;

        /// <summary>
        /// 绑定数据集到数据表控件
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="getDataSource"></param>
        /// <param name="sql"></param>
        /// <param name="pageid"></param>
        /// <param name="pagesize"></param>
        public void BindControl(DataGridView dgv, GetDataSource getDataSource, string sql = "", int pageid = 1, int pagesize = 20)
        {
            if (sql == null) return;
            this.dataGridView = dgv;
            this.fnc = getDataSource;
            this.sql = sql;

            this.Paging(pageid, pagesize);
        }

        /// <summary>
        /// 执行分页方法
        /// </summary>
        /// <param name="pageid"></param>
        /// <param name="pagesize"></param>
        public void Paging(int pageid, int pagesize)
        {
            if (PagingHandler != null)
            {
                PagingHandler(pageid, pagesize);
            }
            else
            {
                if (this.sql == null) return;
                int recordcount, pagecount;
                this.dataGridView.DataSource = this.fnc(out recordcount, out pagecount, this.sql, pageid, pagesize);
                this.PageCount = pagecount;
                this.RecordCount = recordcount;
            }

            this.PageIndex = pageid;
            this.PageSize = pagesize;

            lblPageIndex.Text = this.PageIndex.ToString();
            lblPageCount.Text = this.PageCount.ToString();
            lblRecordCount.Text = this.RecordCount.ToString();
            tbPageIndex.Text = this.PageIndex.ToString();
            tbPageSize.Text = this.PageSize.ToString();
        }

        /// <summary>
        /// 配置分页参数
        /// </summary>
        /// <param name="recordcount"></param>
        /// <param name="pagecount"></param>
        /// <param name="pageid"></param>
        /// <param name="pagesize"></param>
        public void Config(int recordcount, int pagecount, int pageid = 1, int pagesize = 20)
        {
            this.PageCount = pagecount;
            this.RecordCount = recordcount;

            this.PageIndex = pageid;
            this.PageSize = pagesize;

            lblPageCount.Text = this.PageCount.ToString();
            lblRecordCount.Text = this.RecordCount.ToString();
            tbPageIndex.Text = this.PageIndex.ToString();
            tbPageSize.Text = this.PageSize.ToString();
            lblPageIndex.Text = this.PageIndex.ToString();
        }

        /// <summary>
        /// 获取或设置第一页的显示文本
        /// </summary>
        public string FirstPageText { get { return lnkFirstPage.Text; } set { lnkFirstPage.Text = value; } }

        /// <summary>
        /// 获取或设置上一页的显示文本
        /// </summary>
        public string PrevPageText { get { return lnkPrevPage.Text; } set { lnkPrevPage.Text = value; } }

        /// <summary>
        /// 获取或设置下一页的显示文本
        /// </summary>
        public string NextPageText { get { return lnkNextPage.Text; } set { lnkNextPage.Text = value; } }

        /// <summary>
        /// 获取或设置最末页的显示文本
        /// </summary>
        public string LastPageText { get { return lnkLastPage.Text; } set { lnkLastPage.Text = value; } }

        // 布局外观私有变量
        private string _layout;

        /// <summary>
        /// 获取或设置布局外观
        /// <para>值为 MINI|NORMAL</para>
        /// </summary>
        public string Layout { get { return _layout; } set { _layout = value; SetLayout(); } }

        /// <summary>
        /// 设置布局
        /// <para>值为 MINI|NORMAL</para>
        /// </summary>
        /// <param name="layout"></param>
        private void SetLayout()
        {
            if (_layout == "MINI")
            {
                lbl共.Visible = false;
                lbl每页.Visible = false;
                lbl条.Visible = false;
                lbl页.Visible = false;
                lblRecordCount.Visible = false;
                lbl条记录.Visible = false;
                tbPageSize.Visible = false;
                tbPageIndex.Visible = false;
                lblPageIndex.Visible = true;
            }
            ResizeControlWidth();
        }

    }
}
